//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.plugin.mybatis.generator.tk;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.internal.DefaultCommentGenerator;

import java.util.Properties;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;

public class SimpleCommentGenerator extends DefaultCommentGenerator {
    private static final String APIMODELPROPERTYFLAG = "apiModelPropertyFlag";
    private static final String DTOFLAG = "dtoFlag";
    private static String apiModelPropertyFlag = "";
    private static String dtoFlag = "";
    /**
     * 设置用户配置参数
     */
    public void addConfigurationProperties(Properties properties) {
        // 调用父类方法
        super.addConfigurationProperties(properties);
        // 获取suppressAllComments参数值
        apiModelPropertyFlag = properties.getProperty(APIMODELPROPERTYFLAG);
        dtoFlag = properties.getProperty(DTOFLAG);
    }

    public SimpleCommentGenerator() {
    }


    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if (introspectedTable.getPrimaryKeyColumns().size() == 0) {
            throw new RuntimeException("表"+introspectedTable.getFullyQualifiedTable().getIntrospectedTableName()+"没有设置主键。请设置");
        } else {
            /**字段名是否数据库关键字**/
            boolean isColumnNameDelimited = introspectedColumn.isColumnNameDelimited();

            IntrospectedColumn pkColumn = (IntrospectedColumn)introspectedTable.getPrimaryKeyColumns().get(0);
            String remake = introspectedColumn.getRemarks().replace("\n", "").replace("\r", "").replace('"', '\'');
            field.addJavaDocLine("/**");
            field.addJavaDocLine(" * " + remake);
            field.addJavaDocLine(" * column:" + introspectedColumn.getActualColumnName());
            this.addJavadocTag(field, false);
            field.addJavaDocLine(" */");
            if( !(stringHasValue(apiModelPropertyFlag) && "false".equals(apiModelPropertyFlag)) ){
                field.addAnnotation("@ApiModelProperty(value = \"" + remake + "\")");
            }

            if (introspectedColumn.getActualColumnName().equals(pkColumn.getActualColumnName())) {
                if(!(stringHasValue(dtoFlag) && "true".equals(dtoFlag))){
                    field.addAnnotation("@Id");
                    field.addAnnotation("@GeneratedValue(generator = \"JDBC\")");
                }
            }
            if(!(stringHasValue(dtoFlag) && "true".equals(dtoFlag))){
                /*if("delete_flag".equals(introspectedColumn.getActualColumnName())){
                    field.addAnnotation("@LogicDelete");
                }
                if("version".equals(introspectedColumn.getActualColumnName())){
                    field.addAnnotation("@Version");
                }*/
                if(isColumnNameDelimited){
                    field.addAnnotation("@Column(name = \"`" + introspectedColumn.getActualColumnName() + "`\")");
                }else {
                    field.addAnnotation("@Column(name = \"" + introspectedColumn.getActualColumnName() + "\")");
                }
            }

            if(stringHasValue(introspectedColumn.getTypeHandler())){
                field.addAnnotation("@ColumnType(typeHandler = "+introspectedColumn.getTypeHandler()+".class)");
            }
        }
    }
}
